package com.itheima.xx;

import com.itheima.xx.dao.UserMapper;
import com.itheima.xx.pojo.User;
import com.itheima.xx.util.SqlSessionUtil1;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Description:
 *
 * @author codermao
 * @date 2023/10/25 15:13
 */
public class UserMapperTest2 {
    @Test
    public void test01() {
        SqlSession session = SqlSessionUtil1.getSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);

        userMapper.deleteUser(7);

        SqlSessionUtil1.commitAndClose(session);
    }

    @Test
    public void test02() {
        SqlSession session = SqlSessionUtil1.getSession();

        UserMapper userMapper = session.getMapper(UserMapper.class);

        User user = userMapper.queryUserByUserNameAndSex("梦三息", "男");

        System.out.println(user);

        session.close();
    }


    @Test
    public void test03() {
        SqlSession session = SqlSessionUtil1.getSession();

        UserMapper userMapper = session.getMapper(UserMapper.class);

        HashMap<String, String> map = new HashMap<>();
        map.put("userName", "孙悟空");
        map.put("sex", "男");

        User user = userMapper.queryUserByMap(map);
        System.out.println(user);

        session.close();
    }

    @Test
    public void test04() {
        SqlSession session = SqlSessionUtil1.getSession();

        UserMapper userMapper = session.getMapper(UserMapper.class);

        User user = new User(null, "李相夷", new Date(89, 3, 4), "男", "四顾门");

        userMapper.addUser(user);

        System.out.println("user.id = " + user.getId());

        session.close();
    }

    @Test
    public void test05() {
        SqlSession session = SqlSessionUtil1.getSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        Integer count = userMapper.queryUserCount("user");
        System.out.println("表中数据量：" + count);
        session.close();
    }

    @Test
    public void test06() {
        SqlSession session = SqlSessionUtil1.getSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        List<User> list = userMapper.queryUserByLikeUserName("空");
        for (User user : list) {
            System.out.println("user --> " + user);
        }
        session.close();
    }

    @Test
    public void test07() {
        SqlSession session = SqlSessionUtil1.getSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        Map<String, Object> map = userMapper.queryUserByIdReturnMap(1);
        Set<String> keys = map.keySet();
        for (String key : keys) {
            System.out.println(key + "--" + map.get(key));
        }

        session.close();
    }

    @Test
    public void selectReturnMap() {
        SqlSession session = SqlSessionUtil1.getSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        Map<Integer, User> map = userMapper.queryReturnMap();
        Set<Integer> keys = map.keySet();
        for (Integer key : keys) {
            System.out.println(key + "--" + map.get(key));
        }
        session.close();
    }

    @Test
    public void queryUserById2() {
        SqlSession session = SqlSessionUtil1.getSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        User user = userMapper.queryUserById2(9);
        System.out.println(user);

        session.close();
    }

}
